package com.yuanfengPlus.basicInfoManage.logisticscompany.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yuanfengPlus.basicInfoManage.logisticscompany.dao.ILogisticsCompanyMapper;
import com.yuanfengPlus.basicInfoManage.logisticscompany.model.LogisticsCompanyInfo;
import com.yuanfengPlus.basicInfoManage.logisticscompany.service.ILogisticsCompanyService;
import com.yuanfengPlus.utils.ObjectValidator;
import com.yuanfengPlus.utils.PageResult;
import com.yuanfengPlus.utils.Result;
import com.yuanfengPlus.utils.SnowflakeIdWorker;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Map;

@Service
public class LogisticsCompanyServiceImpl implements ILogisticsCompanyService {
    @Resource
    private ILogisticsCompanyMapper ilocm;
    @Override
    public Result<String> insert(LogisticsCompanyInfo logisticsCompanyInfo) {
        Map<String, String> fieldErrorMessages = Map.ofEntries(
                Map.entry("logistics_companies_name", "物流公司名称不能为空"),
                Map.entry("logistics_companies_remark", "物流公司备注不能为空"),
                Map.entry("logistics_companies_contact_person", "物流公司联系人不能为空"),
                Map.entry("logistics_companies_contact_phone", "物流公司联系电话不能为空"),
                Map.entry("logistics_companies_contact_region", "物流公司联系地区不能为空"),
                Map.entry("logistics_companies_contact_address", "物流公司联系地址不能为空")
        );
        // 调用通用的工具类进行非空检查
        if (!ObjectValidator.checkObject(logisticsCompanyInfo, fieldErrorMessages)) {
            return Result.failure(500,"物流信息对象中存在字段为空");
        }
        // 生成编号
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(1);
        logisticsCompanyInfo.setLogistics_companies_ID(snowflakeIdWorker.nextId());;
        // 设置创建时间
        logisticsCompanyInfo.setLogistics_companies_operate_time(new Timestamp(System.currentTimeMillis()));
        // 设置状态
        logisticsCompanyInfo.setLogistics_companies_state(0);
        if(ilocm.insert(logisticsCompanyInfo) <= 0){
            return Result.failure(500,"添加失败");
        }
        return Result.success(" 添加成功");
    }

    @Override
    public Result<String> update(LogisticsCompanyInfo logisticsCompanyInfo) {
        if(ilocm.update(logisticsCompanyInfo) <= 0){
            return Result.failure(500,"修改失败");
        }
        return Result.success("修改成功");
    }

    @Override
    public PageResult selectAll(Map<String, Object> params) {
        // 检查并设置默认值
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);

        // 确保 pageNum 和 pageSize 不为 null
        if (pageNum == null || pageSize == null) {
            throw new IllegalArgumentException("pageNum 和 pageSize 不能为 null");
        }

        PageHelper.startPage(pageNum, pageSize);

        // 确保 ilocm 不为 null
        if (ilocm == null) {
            throw new IllegalStateException("ilocm 未正确初始化");
        }

        Page<LogisticsCompanyInfo> page = ilocm.selectAll(params);

        // 确保 page 不为 null
        if (page == null) {
            throw new IllegalStateException("查询结果为空");
        }

        return new PageResult(page.getTotal(), page.getResult());
    }


    @Override
    public Result<String> delete(String logistics_companies_ID) {
        return null;
    }
}
